<?php
namespace app\api\controller;
use qiniu\Qn;
use think\facade\Db;
class Img extends Base{

    //获取列表
    public function get_list(){
        $param = input('');
        $prefix = $param['prefix'] ? ['prefix' => $param['prefix']]:[];
        $list = Db::table('an_img')
        ->order('id desc')
        ->page($param['page'],$param['limit'])
        ->where($prefix)
        ->select();
        $this->info['data'] = [
            'list' => $list,
        ];
        $this->info['total'] = Db::table('an_img')
        ->where($prefix)
        ->count();
        return json($this->info);
    }

    //删除数据库中 七牛云文件
    public function del_data(){
        $param = input('');
        Db::table('an_img')->where('id',$param['id'])->delete();
        return json($this->info);
    }

    //数据库中 插入七牛云图片记录
    public function up_data(){
        $param = input('');
        Db::table('an_img')->insert([
            'create_time'=>time(),
            'prefix'=>$param['prefix'],
            'key'=>$param['key'],
        ]);
        return json($this->info);
    }

    //上传base64 到服务器
    public function imgBase64ToFile() {
        $param = input('');
        $type = "png";
        if(strstr($param['img'], 'data:image/png;base64')){
            $base_img = str_replace('data:image/png;base64,','', $param['img']);
            $type = "png";
        }

        if(strstr($param['img'], 'data:image/jpg;base64')){
            $base_img = str_replace('data:image/jpg;base64,','', $param['img']);
            $type = "jpg";
        }

        if(strstr($param['img'], 'data:image/jpeg;base64')){
            $base_img = str_replace('data:image/jpeg;base64,','', $param['img']);
            $type = "jpeg";
        }
        
        $path = 'uploads/'.date("Ymd").'/local'.date("YmdHis").'.'.$type;
        $res = file_put_contents($path, base64_decode($base_img));
        $path = str_replace("uploads/","",$path);
        if($res){
            Db::table('an_img')->insert([
                'create_time'=>time(),
                'prefix'=>'local',
                'key'=>$path,
            ]);
            $this->info['msg'] = '上传成功';
            $this->info['data'] = $path;
            
        }else{
            $this->info['code'] = 201;
            $this->info['msg'] = '上传失败';
        }
        return json($this->info);
    }

    //上传图片到服务器
    public function upImg(){
        $file = request()->file('file');
        $savename = \think\facade\Filesystem::disk('public')->putFile('uploads', $file);
        $savename = str_replace("\\","/",$savename);
        $savename = str_replace("uploads/","",$savename);

        Db::table('an_img')->insert([
            'create_time'=>time(),
            'prefix'=>'local',
            'key'=>$savename,
        ]);
       
        $this->info['msg'] = '上传成功';
        $this->info['data'] = $savename;
        return json($this->info);
    }

    //删除服务器文件
    public function del_server_img(){
        $pt = input('');
        $path = './uploads/'.$pt['path'];
        if (!unlink($path)){
            $this->info['msg'] = '删除失败';
            $this->info['code'] = 201;
            return json($this->info);
        }else{
            Db::table('an_img')->where('id',$pt['id'])->delete();
            $this->info['msg'] = '删除成功';
            return json($this->info);
        }
    }
}
